package geguol.cc.widgetapplication.activity;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import geguol.cc.widgetapplication.R;
import geguol.cc.widgetapplication.bean.Topic;

/**
 * 1. 集合中封装数据
 * 2. 根据集合中数据填充不同的对象
 */
public class TabedActivity extends AppCompatActivity {
	private ViewPager mViewPager;
	private Context mContext;
	private List<Topic> list = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_tabed);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);

		initData();

		mViewPager = (ViewPager) findViewById(R.id.container);
		mViewPager.setAdapter(new MpagerAdapter());
		FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
		fab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
						.setAction("Action", null).show();
			}
		});



	}

	private void initData() {
		for (int i = 0; i < 6; i++) {
			Topic topic  =  new Topic(i,i+"，我是标题","我是内容");
			list.add(topic);
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_tabed, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	class MpagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view==object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Topic temp = list.get(position);
			View view = getView(temp);
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	private View getView(Topic bean){
		View mRootPager;
		TextView title,content;
		if (bean.type%2==0){
			mRootPager = View.inflate(mContext, R.layout.layout1, null);
			title = (TextView) mRootPager.findViewById(R.id.title);
			content= (TextView) mRootPager.findViewById(R.id.content);
		}else {
			mRootPager = View.inflate(mContext, R.layout.layout2, null);
			title = (TextView) mRootPager.findViewById(R.id.title);
			content= (TextView) mRootPager.findViewById(R.id.content);
		}

		title.setText(bean.title);
		content.setText(bean.content);

		return mRootPager;
	}
}
